package com.zjt.takeoutproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zjt.takeoutproject.common.CustomException;
import com.zjt.takeoutproject.common.R;
import com.zjt.takeoutproject.entity.Category;
import com.zjt.takeoutproject.entity.Dish;
import com.zjt.takeoutproject.entity.Setmeal;
import com.zjt.takeoutproject.mapper.CategoryMapper;
import com.zjt.takeoutproject.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjt.takeoutproject.service.DishService;
import com.zjt.takeoutproject.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2023-06-13
 */
@Slf4j
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private DishService dishService;

    //根据id删除分类，删除前需要判断
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        //添加查询条件
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(dishLambdaQueryWrapper);
        //查询当前分类是否关联菜品，如果关联抛出异常
        if(count1>0){
            throw new CustomException("当前分类下关联了菜品，请删除对应菜品后再进行删除");
        }
        //查询当前分类是否关联套餐，如果关联抛出异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(setmealLambdaQueryWrapper);
        if (count2>0){
            throw new CustomException("当前分类下关联了套餐，请删除对应套餐后再进行删除");
        }
        //正常删除
        super.removeById(id);
    }

}
